// 图表: 资产面积分类
import * as $echarts from '../../../components/ec-canvas/echarts'
import $chartOption from '../../../data/chartOption.js'

const $app = getApp()

function initChart(canvas, width, height, dpr) {
  let chart = $echarts.init(canvas, null, {
    width: width,
    height: height,
    devicePixelRatio: dpr // new
  })
  canvas.setChart(chart)
  // 设置图表配置项
  let option = {
    title: { text: '资产面积分类' },
    legend: { // ===== 图例组件 =====
      ...$chartOption.legend,
      orient : 'vertical',  
      right: 0,
      top: 'center'
    },
    tooltip: $chartOption.tooltip_pie, // ===== 提示框组件 =====
    series: [{
      type: 'pie',
      center: ['40%', '50%'],
      radius: '50%',
      label: {
        show: true,
        position: 'outside',
        formatter: '{b}\n{d}%',
        fontWeight: 400
      },
      data: []
    }],
    color: $chartOption.color,
    textStyle: $chartOption.textStyle // ===== 全局的字体样式 =====
  }
  // 获取资产面积数据
  $app.$api.data.getAssetarea({}).then(({ data }) => {
    console.log('api-资产面积分类', data)
    let newData = data || []
    newData.forEach(b => {
      option.series[0].data.push({
        name: b.assetsType,
        value: b.allsum
      })
    })
    chart.setOption(option)
  }).catch((err) => {})
  return chart
}

Component({
  data: {
    ec: { onInit: initChart }
  }
})
